// pages/biaodan/biaodan.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formFirstSetting: false,
    red: '../../images/red.png',
    green: '../../images/green.png',
    yellow: '../../images/yellow.png', //20.89
    sg: 0, //cm
    tz: 0, //g
    mbtz: 0, //kg
    // gmy:'无', 
    // tsjb:'无', 
    // mxjb:'无', 
    btnIndex: 0,
    list: [{
        title: '修身型',
        color: '#3D83E5',
        active: true,
      },
      {
        title: '特殊性',
        color: '#57BF6A',
        active: false,
      },
      {
        title: '养生型',
        color: '#ECA150',
        active: false,
      },

    ],
    btnList: ['经期', '备孕', '怀孕', '哺乳'],
    ysList: [{
      title: '过敏原',
      value: ""
    }, {
      title: '特殊疾病',
      value: ""
    }, {
      title: '慢性疾病',
      value: ""
    }],
  },
  getUserInfo() {
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: 'https://www.daizhongdai.xyz:3007/getuserinfo',
      header: {
        sessiontoken: wx.getStorageSync('token')
      },
      success: res => {
        console.log(res.data.data.data[0]);
        var d = res.data.data.data[0]
        var info = JSON.parse(d.special_type)
        var list = this.data.list
        var ysList = this.data.ysList
        for (const key in info) {
          list[key].active = info[key].active
          ysList[key].value = info[2].content[key]
        }
        var btnIndex = this.data.btnIndex
        for (const key in this.data.btnList) {
          if (this.data.btnList[key] == info[1].title)
            btnIndex = key
        }
        this.setData({
          sg: d.height,
          tz: d.weight,
          mbtz: info[0].mbtz,
          list,
          btnIndex,
          ysList
        })
      },
      fail: err => {
        console.log(err);
        wx.showToast({
          title: '出错了..',
          icon: 'error'
        })
      },complete:()=>{
        wx.hideLoading({
          success: (res) => {},
        })
      }
    })
  },
  tapCheckbox: function (e) {
    // console.log(e.currentTarget.dataset.i);
    var i = e.currentTarget.dataset.i
    var d = this.data.list
    d[i].active = !d[i].active
    this.setData({
      list: d
    })
    var cnt = 0
    for (var ii = 0; ii < d.length; ii++) {
      if (d[ii].active) {
        cnt++
      }
    }
    console.log(cnt);
    if (cnt == 0) {
      wx.showModal({
        title: '请至少选择一种类型',
      })
      d[i].active = true
      this.setData({
        list: d
      })
    }



  },
  go: function (e) {
    this.saveUserData(1)
  },
  saveUserData: function (t) {
    console.log(this.data);
    var that = this
console.log(this.data.ysList[0].value+"999999999999999999");
    wx.request({
      url: "https://www.daizhongdai.xyz:3007/perfect/user",
      method: 'GET',
      header: {
        sessiontoken: wx.getStorageSync('token')
      },
      data: {
        gender: that.data.gender,
        height: that.data.sg,
        weight: that.data.tz,
        kouwen: that.data.flavor,
        birth: that.data.birth,
        special_type: JSON.stringify([{
          active: this.data.list[0].active,
          mbtz: that.data.mbtz
        }, {
          active: this.data.list[1].active,
          title: this.data.btnList[this.data.btnIndex]
        }, {
          active: this.data.list[2].active,
          content: [this.data.ysList[0].value, this.data.ysList[1].value, this.data.ysList[2].value]
        }]),
      },
      success: res => {
        console.log(res.data);
        // this.saveUserData()
        if(t==1){
          wx.switchTab({
            url: '../index/index',
          })
        }
    
      },
      fail: err => {
        console.log(err);
        if(t==1){
          wx.showToast({
            title: '出错了...',
          })
        }
     
      }
    })
  },
  getBmi() {
    var bmi = (this.data.tz / (this.data.sg * this.data.sg)).toFixed(2)
    console.log(bmi);
  },
  changeBtn(e) {
    this.setData({
      btnIndex: e.currentTarget.dataset.index
    })
    wx.setStorageSync('btnIndex', e.currentTarget.dataset.index)
  },
  clickMe(e) {
    wx.showToast({
      title: '已提交',
      icon: 'success'
    })
  },
  alertMsg: function (e) {
    var index = e.currentTarget.dataset.index
    console.log(index);
    if (index == 0) {
      wx.navigateTo({
        url: '../alertOne/alertOne',
      })
    } else if (index == 2) {
      wx.navigateTo({
        url: '../alertThree/alertThree',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.getUserInfo()
    if (options.t == "f") {
      this.setData({
        formFirstSetting: true
      })
    }
    this.setData({
      gender: options.gender,
      sg: options.height,
      tz: options.weight,
      flavor: options.flavor,
      birth: options.birth,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var info = wx.getStorageSync('userInfoF')
    console.log(info.sg);
    // this.setData({
    // })

    // var ysList = wx.getStorageSync('ysList')
    // if (ysList.length == 3) {
    //   this.setData({
    //     ysList,
    //   })
    // }
    // this.setData({
    //   btnIndex: wx.getStorageSync('btnIndex'),
    //   sg: info.sg,
    //   tz: info.tz,
    //   mbtz: info.mbtz
    // })
    // console.log(this.data.sg);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log('保存');
    this.saveUserData(0)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})